/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.Estabelecimento_cont;
import control.Local_cont;
import javax.swing.table.DefaultTableModel;
import model.Estabelecimento;
import model.Local;

/**
 *
 * @author Diego
 */
public class JInternalFrame_estabelecimento_cont extends javax.swing.JInternalFrame {

    /**
     * Creates new form JInternalFrame_estabelecimento
     */
    public JInternalFrame_estabelecimento_cont() {
        initComponents();
    }

    public void listar() {
        modelo = (DefaultTableModel) Tabela_estabelecimento.getModel();
        modelo.setNumRows(0);
        estab_cont = new Estabelecimento_cont();
        for (int i = 0; i < estab_cont.read().size(); i++) {
            modelo.addRow(new Object[]{estab_cont.read().get(i).getIdestabelecimento(),
                        estab_cont.read().get(i).getNome(),
                        estab_cont.read().get(i).getEndereco(),
                        estab_cont.read().get(i).getTelefone(),
                        estab_cont.read().get(i).getEmail(),
                        estab_cont.read().get(i).getIdlocal()});
        }
    }
    private DefaultTableModel modelo;
    private Estabelecimento estab;
    private Estabelecimento_cont estab_cont;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Tabela_estabelecimento = new javax.swing.JTable();
        btn_excluir = new javax.swing.JButton();
        btn_novo = new javax.swing.JButton();
        btn_sair = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txt_estabelecimento = new javax.swing.JTextField();
        jcmbx_local = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        Tabela_estabelecimento.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id estabaelecimento", "Nome", "Endereço", "Telefone", "Email", "Local"
            }
        ));
        Tabela_estabelecimento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Tabela_estabelecimentoMouseClicked(evt);
            }
        });
        Tabela_estabelecimento.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                Tabela_estabelecimentoAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        Tabela_estabelecimento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                Tabela_estabelecimentoKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(Tabela_estabelecimento);

        btn_excluir.setText("Excluir");
        btn_excluir.setEnabled(false);
        btn_excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_excluirActionPerformed(evt);
            }
        });

        btn_novo.setText("Novo");
        btn_novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_novoActionPerformed(evt);
            }
        });

        btn_sair.setText("Sair");
        btn_sair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_sairActionPerformed(evt);
            }
        });

        jLabel1.setText("Digite o nome do estabelecimento para busca-lo");

        txt_estabelecimento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_estabelecimentoKeyReleased(evt);
            }
        });

        jcmbx_local.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jcmbx_localMouseEntered(evt);
            }
        });

        jLabel2.setText("Local");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_sair)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_novo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_excluir))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txt_estabelecimento))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 309, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jcmbx_local, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_estabelecimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcmbx_local, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_excluir)
                    .addComponent(btn_novo)
                    .addComponent(btn_sair))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_novoActionPerformed
        modelo = (DefaultTableModel) Tabela_estabelecimento.getModel();
        modelo.addRow(new Object[WIDTH]);
    }//GEN-LAST:event_btn_novoActionPerformed

    private void Tabela_estabelecimentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Tabela_estabelecimentoMouseClicked
        btn_excluir.setEnabled(true);
    }//GEN-LAST:event_Tabela_estabelecimentoMouseClicked

    private void btn_sairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_sairActionPerformed
        dispose();
    }//GEN-LAST:event_btn_sairActionPerformed

    private void Tabela_estabelecimentoAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_Tabela_estabelecimentoAncestorAdded
        listar();
    }//GEN-LAST:event_Tabela_estabelecimentoAncestorAdded

    private void jcmbx_localMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jcmbx_localMouseEntered
        local_cont = new Local_cont();
        local = new Local();
        jcmbx_local.removeAllItems();
        for (int i = 0; i < local_cont.read().size(); i++) {
            jcmbx_local.addItem(local_cont.read().get(i).getIdlocal() + " > " + local_cont.read().get(i).getNome());
        }
    }//GEN-LAST:event_jcmbx_localMouseEntered

    private void Tabela_estabelecimentoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Tabela_estabelecimentoKeyReleased
        estab = new Estabelecimento();
        estab_cont = new Estabelecimento_cont();
        if (Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 0) == null) {
            estab.setNome(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 1).toString());
            estab.setEndereco(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 2).toString());
            estab.setTelefone(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 3).toString());
            estab.setEmail(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 4).toString());
            estab.setIdlocal(Integer.valueOf(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 5).toString()));
            estab_cont.create(estab);
        }else{
            estab.setIdestabelecimento(Integer.valueOf(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 0).toString()));
            estab.setNome(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 1).toString());
            estab.setEndereco(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 2).toString());
            estab.setTelefone(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 3).toString());
            estab.setEmail(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 4).toString());
            estab.setIdlocal(Integer.valueOf(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 5).toString()));
            estab_cont.update(estab);
        }
        listar();
        btn_excluir.setEnabled(false);
    }//GEN-LAST:event_Tabela_estabelecimentoKeyReleased

    private void btn_excluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_excluirActionPerformed
        estab = new Estabelecimento();
        estab_cont = new Estabelecimento_cont();
        modelo = (DefaultTableModel) Tabela_estabelecimento.getModel();
        if(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 0)==null){
            modelo.removeRow(WIDTH);
        }else{
            estab.setIdestabelecimento(Integer.valueOf(Tabela_estabelecimento.getValueAt(Tabela_estabelecimento.getSelectedRow(), 0).toString()));
            estab_cont.delete(estab);
        }
        listar();
        btn_excluir.setEnabled(false);
    }//GEN-LAST:event_btn_excluirActionPerformed

    private void txt_estabelecimentoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_estabelecimentoKeyReleased
        modelo = (DefaultTableModel) Tabela_estabelecimento.getModel();
        modelo.setNumRows(0);
        estab = new Estabelecimento();
        estab_cont = new Estabelecimento_cont();
        estab.setNome(txt_estabelecimento.getText());
        if (txt_estabelecimento.getText().equals("")) {
            listar();
            btn_excluir.setEnabled(false);
        } else {
        for (int i = 0; i < estab_cont.search(estab).size(); i++) {
            modelo.addRow(new Object[]{
                        estab_cont.search(estab).get(i).getIdestabelecimento(),
                        estab_cont.search(estab).get(i).getNome(),
                        estab_cont.search(estab).get(i).getEndereco(),
                        estab_cont.search(estab).get(i).getTelefone(),
                        estab_cont.search(estab).get(i).getEmail(),
                        estab_cont.search(estab).get(i).getIdlocal()});
          }
        }
    }//GEN-LAST:event_txt_estabelecimentoKeyReleased
    private Local_cont local_cont;
    private Local local;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tabela_estabelecimento;
    private javax.swing.JButton btn_excluir;
    private javax.swing.JButton btn_novo;
    private javax.swing.JButton btn_sair;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcmbx_local;
    private javax.swing.JTextField txt_estabelecimento;
    // End of variables declaration//GEN-END:variables
}
